package sing.rabbit.direct.common;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 *
 *                           ↱ "dev"      → directQueue1 () → Consumer      <br>
 * Provider → directExchange → "dev,prod" → directQueue2 () → Consumer     <br>
 *                           ↳ "test"     → directQueue3 () → Consumer     <br>
 *
 *
 * @author xing
 * @date 2022/9/8 6:31
 */
@Configuration
public class DirectConfigurations {
    public static final String DIRECT_QUEUE_NAME_1 = "direct-queue-1";
    public static final String DIRECT_QUEUE_NAME_2 = "direct-queue-2";
    public static final String DIRECT_QUEUE_NAME_3 = "direct-queue-3";

    public static final String DIRECT_EXCHANGE_NAME = "direct-exchange";

    public static final String DIRECT_ROUTING_KEY_TEST = "test";
    public static final String DIRECT_ROUTING_KEY_PROD = "prod";
    public static final String DIRECT_ROUTING_KEY_DEV = "dev";

    @Bean
    public Queue directQueue1() {
        return new Queue(DIRECT_QUEUE_NAME_1);
    }

    @Bean
    public Queue directQueue2() {
        return new Queue(DIRECT_QUEUE_NAME_2, true, false, false);
    }

    @Bean
    public Queue directQueue3() {
        return new Queue(DIRECT_QUEUE_NAME_3, true, false, false,new HashMap<>(){{
            // 声明队列时，对队列设置的参数
            // put("x-max-length" , 5);
        }});
    }

    @Bean
    public DirectExchange directExchange() {
        return ExchangeBuilder
                .directExchange(DIRECT_EXCHANGE_NAME)
                .durable(true)
                .build();
    }

    @Bean
    public Binding directBinding1() {
        return BindingBuilder
                .bind(directQueue1())
                .to(directExchange())
                .with(DIRECT_ROUTING_KEY_DEV);
    }

    @Bean
    public Binding directBinding2(Queue directQueue2, DirectExchange directExchange) {
        return BindingBuilder
                .bind(directQueue2)
                .to(directExchange)
                .with(DIRECT_ROUTING_KEY_PROD);
    }

    @Bean
    public Binding directBinding22(Queue directQueue2, DirectExchange directExchange) {
        return BindingBuilder
                .bind(directQueue2)
                .to(directExchange)
                .with(DIRECT_ROUTING_KEY_DEV);
    }

    @Bean
    public Binding directBinding3(Queue directQueue3, DirectExchange directExchange) {
        return BindingBuilder
                .bind(directQueue3)
                .to(directExchange)
                .with(DIRECT_ROUTING_KEY_TEST);
    }
}
